package com.chance.cc.crawler.development.command.schedule;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.development.command.HttpCommand;
import com.chance.cc.crawler.core.downloader.HttpConstant;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequest;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * created by CC on 2020/11/26
 * mail 279020185@qq.com
 */
@Slf4j
public class CrawlerJobScheduleCommand extends HttpCommand {

    public CrawlerJobScheduleCommand(String host, int port){
        super(host,port);
    }

    /**
     * 发布作业
     * @param triggerKey
     * @return
     */
    public HttpPage publishCrawlerJob(String triggerKey ,String deviceName){
        String url = commandUrl("publishCrawlerJob");

        Map<String,String> triggerInfo = new HashMap<>();
        triggerInfo.put("triggerKey",triggerKey);
        triggerInfo.put("deviceName",deviceName);

        //generate request
        HttpRequest request = new HttpRequest();
        request.setUrl(url);
        request.setMethod(HttpConstant.Method.POST);
        request.setRequestBody(HttpRequestBody.json(JSON.toJSONString(triggerInfo),"utf-8"));
        HttpPage httpPage = doCommand(request);
        log.info("trigger info publish result :{}",httpPage.getRawText());
        return httpPage;
    }

    public HttpPage republishCrawlerJob(String triggerKey,String deviceName ){
        String url = commandUrl("republishCrawlerJob");

        Map<String,String> triggerInfo = new HashMap<>();
        triggerInfo.put("triggerKey",triggerKey);
        triggerInfo.put("deviceName",deviceName);

        //generate request
        HttpRequest request = new HttpRequest();
        request.setUrl(url);
        request.setMethod(HttpConstant.Method.POST);
        request.setRequestBody(HttpRequestBody.json(JSON.toJSONString(triggerInfo),"utf-8"));
        HttpPage httpPage = doCommand(request);
        log.info("trigger info republish result :{}",httpPage.getRawText());
        return httpPage;
    }

    public HttpPage deleteCrawlerJob(String triggerKey,String deviceName ){
        String url = commandUrl("deleteSchedulerCrawlerJob");

        Map<String,String> triggerInfo = new HashMap<>();
        triggerInfo.put("triggerKey",triggerKey);
        triggerInfo.put("deviceName",deviceName);

        //generate request
        HttpRequest request = new HttpRequest();
        request.setUrl(url);
        request.setMethod(HttpConstant.Method.POST);
        request.setRequestBody(HttpRequestBody.json(JSON.toJSONString(triggerInfo),"utf-8"));
        HttpPage httpPage = doCommand(request);
        log.info("trigger info delete result :{}",httpPage.getRawText());
        return httpPage;
    }

    @Override
    public String initUrlPrefix(String host,int port) {
        return StringUtils.join("http://",host,":",port,"/crawler/schedule/api/v1");
    }
}
